/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pv112.cranesimulator.utils;

import java.awt.event.KeyEvent;
import java.util.Locale;
import javax.swing.KeyStroke;
import static java.awt.event.KeyEvent.VK_ALT;
import static java.awt.event.KeyEvent.VK_AMPERSAND;
import static java.awt.event.KeyEvent.VK_ASTERISK;
import static java.awt.event.KeyEvent.VK_AT;
import static java.awt.event.KeyEvent.VK_BACK_QUOTE;
import static java.awt.event.KeyEvent.VK_BACK_SLASH;
import static java.awt.event.KeyEvent.VK_BRACELEFT;
import static java.awt.event.KeyEvent.VK_BRACERIGHT;
import static java.awt.event.KeyEvent.VK_CIRCUMFLEX;
import static java.awt.event.KeyEvent.VK_CLOSE_BRACKET;
import static java.awt.event.KeyEvent.VK_COLON;
import static java.awt.event.KeyEvent.VK_COMMA;
import static java.awt.event.KeyEvent.VK_CONTROL;
import static java.awt.event.KeyEvent.VK_DOLLAR;
import static java.awt.event.KeyEvent.VK_EQUALS;
import static java.awt.event.KeyEvent.VK_EURO_SIGN;
import static java.awt.event.KeyEvent.VK_EXCLAMATION_MARK;
import static java.awt.event.KeyEvent.VK_GREATER;
import static java.awt.event.KeyEvent.VK_INVERTED_EXCLAMATION_MARK;
import static java.awt.event.KeyEvent.VK_LEFT_PARENTHESIS;
import static java.awt.event.KeyEvent.VK_LESS;
import static java.awt.event.KeyEvent.VK_MINUS;
import static java.awt.event.KeyEvent.VK_NUMBER_SIGN;
import static java.awt.event.KeyEvent.VK_OPEN_BRACKET;
import static java.awt.event.KeyEvent.VK_PERIOD;
import static java.awt.event.KeyEvent.VK_PLUS;
import static java.awt.event.KeyEvent.VK_QUOTE;
import static java.awt.event.KeyEvent.VK_QUOTEDBL;
import static java.awt.event.KeyEvent.VK_RIGHT_PARENTHESIS;
import static java.awt.event.KeyEvent.VK_SEMICOLON;
import static java.awt.event.KeyEvent.VK_SLASH;
import static java.awt.event.KeyEvent.VK_UNDEFINED;
import static java.awt.event.KeyEvent.VK_UNDERSCORE;

/**
 *
 * @author Michal Ďuriš
 */
public class KeyCodeKeyTextTransformer {

    private KeyCodeKeyTextTransformer() {
    }

    public static int getKeyCode(char key) {
        return KeyEvent.getExtendedKeyCodeForChar(key);
    }

    public static int getKeyCode(String key) {

        if (key == null || key.isEmpty()) {
            return VK_UNDEFINED;
        }

        if (key.length() == 1) {
            return getKeyCode(key.charAt(0));
        }

        if (KeyEvent.getKeyText(VK_CONTROL).equalsIgnoreCase(key)) {

            return VK_CONTROL;
        } else if (KeyEvent.getKeyText(VK_ALT).equalsIgnoreCase(key)) {

            return VK_ALT;
        } else {

            return KeyStroke.getKeyStroke(
                    key.replace(' ', '_').toUpperCase(Locale.ROOT)).getKeyCode();
        }
    }

    public static String getKeyTextRepresentation(int keyCode) {

        if (keyCode == VK_UNDEFINED) {
            return "";
        }

        switch (keyCode) {

            case VK_COMMA: return ",";
            case VK_PERIOD: return ".";
            case VK_SLASH: return "/";
            case VK_SEMICOLON: return ";";
            case VK_EQUALS: return "=";
            case VK_OPEN_BRACKET: return "[";
            case VK_BACK_SLASH: return "\\";
            case VK_CLOSE_BRACKET: return "]";

            case VK_BACK_QUOTE: return "`";
            case VK_QUOTE: return "'";

            case VK_AMPERSAND: return "&";
            case VK_ASTERISK: return "*";
            case VK_QUOTEDBL: return "\"";
            case VK_LESS: return "<";
            case VK_GREATER: return ">";
            case VK_BRACELEFT: return "{";
            case VK_BRACERIGHT: return "}";
            case VK_AT: return "@";
            case VK_COLON: return ":";
            case VK_CIRCUMFLEX: return "^";
            case VK_DOLLAR: return "$";
            case VK_EURO_SIGN: return "€";
            case VK_EXCLAMATION_MARK: return "!";
            case VK_INVERTED_EXCLAMATION_MARK: return "¡";
            case VK_LEFT_PARENTHESIS: return "(";
            case VK_NUMBER_SIGN: return "#";
            case VK_MINUS: return "-";
            case VK_PLUS: return "+";
            case VK_RIGHT_PARENTHESIS: return ")";
            case VK_UNDERSCORE: return "_";
        }

        return KeyEvent.getKeyText(keyCode);
    }

}
